<?php
namespace Tbf\Bundle\DatabaseBundle\Tests;
use Doctrine\DBAL\Connection;

class MysqlLockTest extends TbfDatabaseBundleTestCase{
    function test1(){
        ob_end_clean();
        /** @var Connection $con1 */
        $con1 = $this->get('doctrine.dbal.default_connection');
        $con2 = new Connection($con1->getParams(),$con1->getDriver());
        $con1->query(<<<'SQL'
drop table if exists tbf_database_test_mysql_lock;
create table tbf_database_test_mysql_lock(
  id int auto_increment,
  num int,
  primary key (id)
)engine=innodb DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
insert into tbf_database_test_mysql_lock (num) values (1),(2);
SQL
);
        //not lock select
        $con1->query('START TRANSACTION');
        $con1->query('select * from tbf_database_test_mysql_lock where id=1');
        $con2->query('START TRANSACTION');
        $con2->query('select * from tbf_database_test_mysql_lock where id=1');
        $con1->query('COMMIT');
        $con2->query('COMMIT');

        //select lock in share mode
        $con1->query('START TRANSACTION');
        $con1->query('select * from tbf_database_test_mysql_lock where id=1 LOCK IN SHARE MODE');
        $con2->query('START TRANSACTION');
        $con2->query('select * from tbf_database_test_mysql_lock where id=1 LOCK IN SHARE MODE');
        $con1->query('COMMIT');
        $con2->query('COMMIT');

        //select for update
        /*
        $con1->query('START TRANSACTION');
        $con1->query('select * from tbf_database_test_mysql_lock where id=1 FOR UPDATE');
        $con2->query('START TRANSACTION');
        $con2->query('select * from tbf_database_test_mysql_lock where id=1 FOR UPDATE'); //block at here forever
        $con1->query('COMMIT');
        $con2->query('COMMIT');
        */
        ob_start();
    }
    function query($sql){
        $this->get('doctrine.dbal.default_connection')->query($sql);
    }

}